﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Diagnostics;
using System.Windows.Media.Imaging;
using System.IO;
using System.Data;

namespace ByrForum
{
    public partial class ArticleListItem : UserControl
    {
        public ArticleListItem()
        {
            InitializeComponent();
        }



        public ArticleData ArticleItemData
        {
            get { return (ArticleData)GetValue(ArticleItemDataProperty); }
            set { SetValue(ArticleItemDataProperty, value); }
        }
        //1539862628
        // Using a DependencyProperty as the backing store for ArticleItemData.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ArticleItemDataProperty =
            DependencyProperty.Register("ArticleItemData", typeof(ArticleData), typeof(ArticleListItem), new PropertyMetadata(null, OnDataChanged));
        static void OnDataChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            ArticleListItem ali = (ArticleListItem)obj;
            ArticleData ad = (ArticleData)e.NewValue;
            if (ad != null)
            {
                Debug.WriteLine( ad.Userid);
                Debug.WriteLine(ad.Content);
                ali.textblockUserId.Text = ad.Userid;
                ali.textBlockUserName.Text = ad.username;
                Uri faceUri = new Uri(ad.ImgSource, UriKind.Absolute);
                BitmapImage bmp = new BitmapImage();
                bmp.UriSource = faceUri;
                ali.imageUser.Source = bmp;
                ali.textblockTitle.Text = ad.Title;


                DateTime dtStart =new DateTime(1970, 1, 1);
                long lTime = long.Parse(ad.TimeStamp + "0000000");
                TimeSpan toNow = new TimeSpan(lTime);
                DateTime dtResult = dtStart.Add(toNow);


                ali.textblockInfo.Text = "发表于" + dtResult.ToString("yyyy-MM-dd HH:mm:ss"); ;
                ali.ContentStackPanel.Children.Clear();
                ScrewText(ali, ad.Content);
                if (ad.AttachList.Count != 0)
                {
                    Debug.WriteLine("attach list count" + ad.AttachList.Count);
                    for (int i = 0; i < ad.AttachList.Count; i++)
                    {
                        Debug.WriteLine(ad.AttachList[i]);
                        try
                        {
                            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(ad.AttachList[i]+ByrApi.appkey);
                            req.Credentials = new NetworkCredential(App.MyInfo.id, App.MyInfo.password);
                            RequestState myState = new RequestState();
                            myState.request = req;
                            myState.articleItem = ali;
                            req.BeginGetResponse(HttpWebRequset_Callback, myState);

                        }
                        catch
                        {
                            Debug.WriteLine("network failed");
                        }
                    }
                }
            }
            else {
               
            }

        }
        static void HttpWebRequset_Callback(IAsyncResult asynchronousResult)
        {
            RequestState state = (RequestState)asynchronousResult.AsyncState;
            ArticleListItem ali = (ArticleListItem)state.articleItem;
            HttpWebRequest hwrq = state.request;
            try
            {
                HttpWebResponse response = (HttpWebResponse)hwrq.EndGetResponse(asynchronousResult);
                Debug.WriteLine("get callback");
                Stream responseStream = response.GetResponseStream();
                ali.Dispatcher.BeginInvoke(
                    () =>
                    {
                        BitmapImage bmp = new BitmapImage();
                        try
                        {
                            bmp.SetSource(responseStream);
                            Image img = new Image();
                            img.Source = bmp;
                            img.Margin = new Thickness(10, 10, 10, 10);
                            ali.ContentStackPanel.Children.Add(img);
                        }
                        catch (Exception exc)
                        {
                            Debug.WriteLine(exc.Message);
                        }
                    }
                    );
                //response.Close();
                //responseStream.Close();
            }
            catch (Exception ee)
            {
                Debug.WriteLine("image get back failed"+ee.Message);
            }
           

            
        }
        static void ScrewText(ArticleListItem ali, string content)
        {
            int sect =(int) (content.Length / 800);
            //ali.ContentStackPanel.Children.Clear();
            TextBlock tb = new TextBlock();
            
            Debug.WriteLine("@@@@@@@@@@@@@@@@@@@@@@@@@sect:" + sect+"content length:"+content.Length);
            if (sect == 0)
            {
                tb.Text = content;
                tb.TextWrapping = TextWrapping.Wrap;
                ali.ContentStackPanel.Children.Add(tb);
                return;
            }
            for (int i = 0; i <= sect ; i++)
            {
                TextBlock tba = new TextBlock();
                if (i != sect)
                {
                    
                    tba.Text = content.Substring(800 * i, 800);
                    tba.TextWrapping = TextWrapping.Wrap;
                }
                else {
                    tba.Text = content.Substring(800 * i);
                    tba.TextWrapping = TextWrapping.Wrap;
                    
                }
                ali.ContentStackPanel.Children.Add(tba);
                
            }
        }

        
        
    }
    
}
